#!/usr/bin/perl
package Utils;
use warnings;
use strict;
use Term::ANSIColor qw(:constants);
$Term::ANSIColor::AUTORESET = 1;

use GlobalConfig;


use Exporter qw(import);
our @EXPORT=qw(utils_log utils_timestamp utils_run_cmd 
debug utils_warn printn utils_error utils_info
utils_alert
);

sub printn{
  my ($txt)=@_;
  print "$txt\n";
}

sub debug{
  my ($txt)=@_;
  if($config->{debug}){
    print BLUE ON_WHITE "DEBUG:"; 
    print " $txt\n"; 
  }
}

sub utils_warn{
  my ($txt)=@_;
  if($config->{debug}){
    print BLUE ON_WHITE "WARN:"; 
    print " $txt\n"; 
  }
}

sub utils_error{
  print RED "ERROR: @_\n";
  exit 1;
}

sub utils_info{
  print BLUE ON_WHITE "INFO:";
  print " @_\n";
}

sub utils_log{
  my($txt,$file)=@_;
  open(F,">>$file");
  print F $txt,"\n";
  close(F);
}

sub utils_timestamp{
  my $timestamp=`date +'%Y_%m_%d_%H.%M.%S'`;
  chomp($timestamp);
  $timestamp;
}

sub utils_run_cmd{
  my ($cmd)=@_;
  debug $cmd if($config->{debug});
  system($cmd);
}

sub utils_run_cmd_faf{
  #run command fire and forget.
  my ($cmd)=@_;
  debug $cmd if($config->{debug});
  `$cmd`;
}

sub utils_alert{
  return if (!$config->{enable_tc_status_alert});
  my ($alert_txt)=@_;
  my $cmd="$config->{alert_cmd} $alert_txt";
  utils_run_cmd_faf("$config->{alert_cmd} $alert_txt");
  
}


